<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ include file="/taglibs.jsp"%>
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c-rt"%>
<s:url value="/images" var="imgPath"/>
<s:url value="/docs/givecoupon_example.csv" var="docUrl"/>
<!-- 月历 -->
<script type="text/javascript" charset="UTF-8" src="/js/formValidator.js"></script>
<script type="text/javascript" charset="UTF-8" src="/js/formValidatorRegex.js"></script>
<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick.js"></script>
<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick-zh-CN.js"></script>
<script type="text/javascript" charset="UTF-8" src="/js/jquery.alphanumeric.js"></script>
<script type="text/javascript" charset="UTF-8" src="/js/yaodian100.common.js"></script>
<link rel="stylesheet" type="text/css" href="/css/smoothness.datepick.css" />
<script type="text/javascript" src="/js/jquery.timePicker.js"></script>
<link rel="stylesheet" type="text/css" href="/css/timePicker.css"/>
		
<script language="javascript">
	$().ready( function() {
		$('#startDate').datepick({minDate: null, showOn: 'both', 
	    	buttonImageOnly: true, buttonImage: '${imgPath}/calendar.gif',numberOfMonths:2,dateFormat:"yy-mm-dd",yearRange: '-50:+50'});
		$('#endDate').datepick({minDate:new Date(), showOn: 'both', 
	    	buttonImageOnly: true, buttonImage: '${imgPath}/calendar.gif',numberOfMonths:2,beforeShow:customRange,dateFormat:"yy-mm-dd",yearRange: '-50:+50'});
		function customRange (){
			var startDt = $('#startDate').val(); //起始日
			var endDt = new Date(parseInt(startDt.substring(0,4),10),parseInt(startDt.substring(5,7),10)-1,parseInt(startDt.substring(8,10),10)); //結束日
			return {minDate:endDt}; 
		}
		
		$("#startTime").timePicker({step:60, startTime:"00:00", endTime:"23:00"});
		$("#endTime").timePicker({step:60, startTime:"00:59", endTime:"23:59"});
		
		$.formValidator.initConfig({formid:"formObj",onerror:function(msg){alert(msg)},onsuccess:function(){return true;}});
		<c-rt:if test="${statusStr == 'INIT'}">
		//栏位资料验证
		$("#startDate").formValidator(<c-rt:if test="${!creatingCoupon}">{empty:true}</c-rt:if>).inputValidator({
			min:10,
			max:10,
			onerror:"起始时间<c-rt:if test="${creatingCoupon}">不能为空值,且</c-rt:if>格式为yyyy-MM-dd" }).compareValidator({
			desid:"endDate",
			operateor:"<",
			onerror:"结束时间必须晚于起始"});
		$("#endDate").formValidator(<c-rt:if test="${!creatingCoupon}">{empty:true}</c-rt:if>).inputValidator({	
			min:10,
			max:10,
			onerror:"结束时间<c-rt:if test="${creatingCoupon}">不能为空值,且</c-rt:if>格式为yyyy-MM-dd" });
		</c-rt:if>
		$("#faceValue").formValidator().inputValidator({
			min:1,
			max:10,
			onerror:"礼券面额不能为空值" }).regexValidator({
			regexp:"money",datatype:"enum",
			onerror:"礼券面额必须是正数，小数点最多一位"});
		$("#amount").formValidator().inputValidator({
			min:1,
			max:10,
			onerror:"数量不能为空值" }).regexValidator({
			regexp:"num1",datatype:"enum",
			onerror:"数量必须是正整数"}).functionValidator({
				fun:function(val,elem){
					var trimmedValue = jQuery.trim(val);
					var numericValue = parseInt(trimmedValue);
					if (numericValue == 0 && $("input[name='uploadFile']").val() == '') {
						return false;
					} 
					return true;
				}
				,onerror:"请输入数量或上传档案"
			});
		$("#minimumProductPrice").formValidator().inputValidator({
			min:1,
			max:10,
			onerror:"最低商品价格不能为空值" }).regexValidator({
			regexp:"money",datatype:"enum",
			onerror:"最低商品价格必须是正数，小数点最多一位"}).functionValidator({
				fun:function(val,elem){
					var trimmedValue = parseInt(jQuery.trim(val),10);
					var overFaceValue = parseInt($("input[name='faceValue']").val(),10) * 2 ;
					<yaodian100:checkPermission permissions="GROUP_A-50-100-100,ROLE_CRM_MANAGER">overFaceValue=parseInt($("input[name='faceValue']").val(),10);</yaodian100:checkPermission>
					if ( trimmedValue <= overFaceValue  ) {
						elem.focus();
						return false;
					} else {
						return true;
					}
					
				}
				,onerror:"最低商品价格必须大於面额 两倍以上"});
			
			$("#applyRangeTypeExtension").formValidator().functionValidator({
				fun:function(val,elem){
					var trimmedValue = jQuery.trim(val);
					var selectedApplyRangeTypeStr = $("input:radio:checked[name='applyRangeTypeStr']");
					
					if(selectedApplyRangeTypeStr.val() != 'ALL' && (trimmedValue == '')) {
						return false;
					}
					return true;
				}
				,onerror:"非全馆适用请输入适用范围"
			});
			
			$("#activityLink").formValidator().functionValidator({
				fun:function(val,elem){
					var trimmedValue = jQuery.trim(val);
					
					if (!elem.disabled) {
						if (!isURL(trimmedValue)) {
							return false;
						}
					}
					return true;
				}
				,onerror:"相关活动连结格式错误"
			});

			$("input:radio[name='budget']").formValidator().functionValidator({
				fun:function(val,elem){
					if( $("input:radio:checked[name='budget']").length==0) {
						return false;
					}
					return true;
				}
				,onerror:"请选择预算归属"
			});
			
		$("#faceValue").numeric({allow:"."});
		$("#minimumProductPrice").numeric({allow:"."});
		
		//相关活动连结欄位控制
		$("[name='ifHasActivityLink']").click(function(){
			var activityLinkInput = $("[name='activityLink']");
			if ($(this).val() == 'false') {
				activityLinkInput.attr("disabled", true);
			} else {
				activityLinkInput.attr("disabled", false);
			}
		});
		
	});	

</script>
<s:actionerror/>
<s:actionmessage/>
<s:fielderror />
<s:form action="save.do" method="post" id="formObj" enctype="multipart/form-data">
<h1>Coupon 维护</h1>
<s:hidden name="couponUuid"/>
<s:hidden name="creatingCoupon"/>
<table class="tb-data" width="100%">
	<tr>
		<th>* 名称<BR>（此内容为用户可见）</th>
		<td>
			<s:textfield name="name" id="name" maxLength="30" size="40" />
			&nbsp;
			(最多三十字)
		</td>
	</tr>
	<tr>
		<th>叙述<BR>（此内容为用户可见）</th>
		<td><s:textfield name="description" maxLength="500" size="40" /></td>
	</tr>
	<tr>
		<th>相关活动连结</th>
		<td>
			<s:if test="%{activityLink == null || activityLink == ''}" >
				<c:set var="hasNoActivityLinkChecked" value="checked" ></c:set>
				<s:set var="activityLinkInputInitiallyDisabled" value="true" ></s:set>
			</s:if>
			<s:else>
				<c:set var="hasActivityLinkChecked" value="checked" ></c:set>
				<s:set var="activityLinkInputInitiallyDisabled" value="false" ></s:set>
			</s:else>
			
			<input type="radio" value="false" name="ifHasActivityLink" ${hasNoActivityLinkChecked} />
			无
			&nbsp;
			<input type="radio" value="true" name="ifHasActivityLink" ${hasActivityLinkChecked} />
			<s:textfield name="activityLink" id="activityLink" maxlength="200" size="65" disabled="%{activityLinkInputInitiallyDisabled}"></s:textfield>
		</td>
	</tr>
	<tr>
		<th>* 适用范围</th>
		<td><s:radio name="applyRangeTypeStr" list="applyRangTypeMenu.options" listKey="key" listValue="value.name" /></td>
	</tr>
	<tr>
		<th>范围设定<br/>(, 分隔; <br/>当适用范围为商品时, 请填入商品编号, <br/>当适用范围为目录时, 请填入目录编号)</th>
		<td>
			<s:textarea name="applyRangeTypeExtension" id="applyRangeTypeExtension" cols="80" rows="5"/>
		</td>
	</tr>
	<c-rt:if test="${creatingCoupon}">
	<tr>
		<th>* 期间 起</th>
		<td>
			<s:textfield name="startDate" id="startDate" ></s:textfield>
			<s:textfield name="startTime" id="startTime" size="5" maxLength="5" ></s:textfield>
		</td>
	</tr>
	<tr>
		<th>* 期间 迄</th>
		<td>
			<s:textfield name="endDate" id="endDate" ></s:textfield>
			<s:textfield name="endTime" id="endTime" size="5" maxLength="5" ></s:textfield>
		</td>
	</tr>
	</c-rt:if>
	<tr>
		<th>* 礼券面额</th>
		<td>
<s:if test="couponUuid == null || couponUuid == ''">
¥ <s:textfield name="faceValue" id="faceValue"/>
</s:if><s:else>
¥ ${faceValue}<s:hidden name="faceValue" id="faceValue"/>
</s:else>
		</td>
	</tr>
	<tr>
		<th>* 最低商品价格</th>
		<td>
			¥ <s:textfield name="minimumProductPrice" id="minimumProductPrice" ></s:textfield><BR>
			<font color="#666666">最低商品价格需为礼券面额的两倍以上，例，礼券面额为100，则最低价格为201以上</font><BR>
			<font color="#FF3300">若低于两倍以下，请先设定一个符合两倍以上的值，再请求RD协助调整</font>
		</td>
	</tr>
	<tr>
		<th>* 数量(若上傳檔案请填 0)</th>
		<td>
		<s:if test="%{creatingCoupon}">
			<s:textfield name="amount" id="amount"/>
		</s:if>
		<s:else>
			${amount}
			<s:hidden name="amount" id="amount"/>
		</s:else>
		</td>
	</tr>
	<tr>
		<th>* 单一帐号限用一次<BR>（限定用户能否使用多张同批礼券）</th>
		<td>
			<s:radio name="isOnce" list="#{true:'&nbsp;是', false:'&nbsp;否'}"></s:radio>
		</td>
	</tr>
	<tr>
		<th>状态</th>
		<td>
			${statusMenu.options[statusStr].name}
			<s:hidden name="statusStr"/>
		</td>
	</tr>
	<tr>
		<th>* 预算归属</th>
		<td><s:radio list="menuBudget.options" name="budget" id="budget" listValue="value.name"/></td>
	</tr>
	<s:if test="%{creatingCoupon}">
		<tr>
			<th>档案</th>
			<td><s:file name="uploadFile" /> <a href="${docUrl}">专用文档下载</a></td>
		</tr>
		<tr>
			<th>是否发送EMAIL</th>
			<td>
			<input type="radio" value="Y" id="sendMail" name="sendMail">
			<label>是</label>
			<input type="radio" value="N" checked="checked" id="sendMail" name="sendMail">
			<label>否</label>
			</td>
		</tr>
	</s:if>
	<s:else>
		<tr>
			<th>建立人</th>
			<td>
				${coupon.createUser} / <fmt:formatDate value="${coupon.createDate}" pattern="yyyy-MM-dd HH:mm:ss" />			
			</td>
		</tr>
		<tr>
			<th>修改者</th>
			<td>
				${coupon.modifyUser} / <fmt:formatDate value="${coupon.modifyDate}" pattern="yyyy-MM-dd HH:mm:ss" />			
			</td>
		</tr>
	</s:else>
</table>
<div class="f-link">
    <s:if test="%{creatingCoupon}">
      <input type="submit" onclick="document.formObj.action='generate.do';" value="建立Coupon">
      <input type="button" onclick="document.location='list.do'" value="取消">
    </s:if>
    <s:else>
      <input type="submit" value="储存修改">
      <input type="button" onclick="document.location='view.do?couponUuid=${couponUuid}'" value="取消">
    </s:else>
</div>
</s:form>
